//获取token,解析token
import jwtDecode from 'jwt-decode'

var TokenStorage = {
    getToken:function (){
        var token =localStorage.getItem('jwtToken');
        return token;
    },
    get: function() {
        var token =localStorage.getItem('jwtToken');
        console.log(token)
        if(token!=null){
            var code = jwtDecode(token);
            var message = code.sub;
            var messageObj = JSON.parse(message);
            var id = messageObj.id;
            var username = messageObj.username;
            var role = messageObj.isUser === "0" ? 'user' : 'admin'; // 修改这里
            return { id: id, username: username, role: role };
        }
        else {
            return {};
        }
    },
    remove: function() {
        localStorage.removeItem('jwtToken');
    }
};

export default TokenStorage;
